package com.asiainfo.ncrm.popup;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.asiainfo.ncrm.R;
import com.asiainfo.ncrm.util.AntConstant;
import com.asiainfo.ncrm.util.FileUtil;
import com.asiainfo.ncrm.util.XUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

public class BottomMenuPopupWindow extends PopupWindow implements OnClickListener, OnDismissListener {
	private Context context;
	private ImageView iv;

	public static final int IMAGE_RESULT_CODE = 0;
	public static final int TAKE_PHOTO_CODE = 1;
	public static final int CHOOSE_PHOTO_CODE = 2;
	
	public static final int RESULT_FRONT= 1;
	
	public static final int RESULT_BACK=2;
	
	public int ResultCode=0;
	
	public String imageName="head.jpg";

	public BottomMenuPopupWindow(Context ctx, ImageView iv,int code) {
		this.context = ctx;
		this.iv = iv;
		this.ResultCode= code;
		init();

	}

	private void init() {
		View view = LayoutInflater.from(context).inflate(R.layout.pop_bottom_menu, null);
		view.findViewById(R.id.menu_bottom_ll_1).setOnClickListener(this);
		view.findViewById(R.id.menu_bottom_ll_2).setOnClickListener(this);
		view.findViewById(R.id.menu_bottom_ll_cancel).setOnClickListener(this);
		setContentView(view);
		setWidth(LayoutParams.MATCH_PARENT);
		setHeight(LayoutParams.WRAP_CONTENT);
		setFocusable(true);
		ColorDrawable dw = new ColorDrawable(0x00);
		setBackgroundDrawable(dw);
		setOnDismissListener(this);
	}

	public void show() {
		showAtLocation(iv, Gravity.BOTTOM, 0, 0);
	}
	public void show(boolean isOther) {
		imageName="other.jpg";
		show();
	}

	@Override
	public void onDismiss() {

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.menu_bottom_ll_1:
			if (FileUtil.isSdcardExisting()) {
				// Intent cameraIntent = new
				// Intent("android.media.action.IMAGE_CAPTURE");
				// cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
				// getImageUri());
				// cameraIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
				// ((Activity) context).startActivityForResult(cameraIntent,
				// TAKE_PHOTO_CODE);
				// Intent intent = new
				// Intent(MediaStore.ACTION_IMAGE_CAPTURE);//
				// "android.media.action.IMAGE_CAPTURE"
				// /***
				// * 需要说明一下，以下操作使用照相机拍照，拍照后的图片会存放在相册中的
				// * 这里使用的这种方式有一个好处就是获取的图片是拍照后的原图
				// * 如果不实用ContentValues存放照片路径的话，拍照后获取的图片为缩略图不清晰
				// */
				// ContentValues values = new ContentValues();
				// intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
				// context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				// values));
				// ((Activity) context).startActivityForResult(intent,
				// TAKE_PHOTO_CODE);
					Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
				cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
				((Activity) context).startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
			} else {
				XUtils.showText( "请插入SD卡！");
			}

			dismiss();
			break;
		case R.id.menu_bottom_ll_2:
			// Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
			// galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
			// galleryIntent.setType("image/*");
			// ((Activity) context).startActivityForResult(galleryIntent,
			// CHOOSE_PHOTO_CODE);
			Intent galleryIntent = new Intent(Intent.ACTION_PICK, null);
			galleryIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
			((Activity) context).startActivityForResult(galleryIntent, CHOOSE_PHOTO_CODE);
			dismiss();
			break;
		case R.id.menu_bottom_ll_cancel:
			dismiss();
			break;

		default:
			break;
		}
	}

	private Uri getImageUri() {
		return Uri.fromFile(new File(AntConstant.FACE_FILE_NAME));
	}

	/**
	 * 裁剪
	 * 
	 * @param uri
	 */
	public void startPhotoZoom(Uri uri) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		intent.putExtra("crop", "true");
//		intent.putExtra("aspectX", 1);
//		intent.putExtra("aspectY", 1);
		
		intent.putExtra("outputX", 150);
		intent.putExtra("outputY", 150);
		intent.putExtra("return-data", true);
		((Activity) context).startActivityForResult(intent, IMAGE_RESULT_CODE);
	}

	public Map<Object, Object> doPhoto(int requestCode,int resultCode, Intent data) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		switch (requestCode) {
		case TAKE_PHOTO_CODE:
			File picture = new File(AntConstant.FACE_FILE_NAME);
			startPhotoZoom(Uri.fromFile(picture));
			map.put(ResultCode, Uri.fromFile(picture).toString() + "");
			break;
		case CHOOSE_PHOTO_CODE:
			startPhotoZoom(data.getData());
			map.put(CHOOSE_PHOTO_CODE, data.getData().toString());
			break;
		case IMAGE_RESULT_CODE:
			Bundle extras = data.getExtras();
			if (null != extras) {
				imageName = (new Date()).getTime()+".jpg";
				map.put(ResultCode, imageName);
				Bitmap photo = extras.getParcelable("data");
				ByteArrayOutputStream stream = new ByteArrayOutputStream();
				photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);
				String imgPath = FileUtil.saveBitToSD(photo, imageName);
//				ImageLoader.getInstance().displayImage("file://" + FileUtil.saveBitToSD(photo, imageName), iv);
				
//				map.put(resultCode, photo);
//				ImageLoader.getInstance().displayImage(photo, iv);
				iv.setImageBitmap(photo);
			}
			break;
		default:
			break;
		}
		return map;

	}

}
